// pages/order/index.js
import { request } from "../../request/request.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs:[{
      id:0,
      value:"全部",
      isActive:true


    },{
        id: 1,
        value: "待付款",
        isActive: false

    },{
        id: 2,
        value: "待发货",
        isActive:false

    },
    {
      id: 3,
      value: "退款/退货",
      isActive:false

  }],
  orderList:[]

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorageSync('token',"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo");
    

  },
  async getOrders(type){
         const res=await request({url:"/my/orders/all",data:{type}});
         console.log(res);
         this.setData({
          orderList:res.data.message.orders.map(v=>({...v,create_time_cn:(new Date(v.create_time*1000).toLocaleString())}))
         });
  },
  changeTitle(index){
          let tabs=this.data.tabs;
          tabs.forEach((v,i)=>i===index?v.isActive=true:v.isActive=false);
          this.setData({
            tabs:tabs
          });
  },
  tabsItemChange(e){
    const index=e.detail.index;
    console.log(index);
    this.changeTitle(index);
    this.getOrders(index+1);
    
 
 
 
   },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

    const token=wx.getStorageSync('token');
    if(!token)
    {
      wx.navigateTo({
        url: '/pages/auth/index',
      })
      return;
    }
    let pages=getCurrentPages();
    let currentPage=pages[pages.length-1];
    const type=currentPage.options.type;
    this.getOrders(type);
    this.changeTitle(type-1);

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})